package com.springboot.cms.kit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2017/4/27.
 */
public class DateKit {

    public static final String date="yyyy-MM-dd";
    public static final String time="HH:mm:ss";
    public static final String datetime="yyyy-MM-dd HH:mm:ss";


    /**
     * 获取当前系统时间戳，精确到毫秒，13位数字
     * @return
     */
    public static long getTimestamp(){
        return System.currentTimeMillis();
    }
    /**
     * 获取当前系统时间戳，精确到秒，10位数字
     * @return
     */
    public static long getUnixTimestamp(){

        return getTimestamp()/1000;
    }

    public static long yyyyMMddHHmmss(String datetime){
        long time=0L;
        try {
            System.out.println("将yyyyMMddHHmmss 字符串转换成秒 当前秒是："+getUnixTimestamp());
            Date date1 = new SimpleDateFormat("yyyyMMddHHmmss").parse(datetime);
            time=date1.getTime()/1000;

        } catch (ParseException e) {
            e.printStackTrace();

        }

        return time;

    }


    public static long parseToTimestamp(String datetime,String format){
        long time=0L;
        try {
            System.out.println("将 "+format +" 字符串转换成秒 当前秒是："+getUnixTimestamp());
            Date date1 = new SimpleDateFormat(format).parse(datetime);
            time=date1.getTime()/1000;

        } catch (ParseException e) {
            e.printStackTrace();

        }

        return time;
    }

    /**
     * 获取今天的开始时间戳 秒
     * @return
     */
    public static long getTodayStartTimestamp(){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis()/1000;
    }
    /**
     * 获取今天的结束时间戳 秒
     * @return
     */
    public static Long getTodayEndTimeTimestamp(){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis()/1000;
    }


    public static long getUnixTimestamp(String datetime,String format){
        if (datetime.length()<=10){
            datetime+=" 00:00:00";
        }
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat(format);
        Date date= null;
        try {
            date = simpleDateFormat .parse(datetime);
            long timeStamp = date.getTime();
            return timeStamp;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

}


